# encoding: utf8
import os
import shutil
import sys
print(os.path.join(os.path.dirname(sys.executable), "Lib", "site-packages"))
# sys.exit()
from PyInstaller.__main__ import run
#
# site-package主要为了traitsui
# lib\gmsh*主要为了gmsh
# Library\bin主要为了vtk

if __name__ == '__main__':
    # [尝试将整个环境全部包含
    opt = [
        # '-D',
        # '-F',
        '-w',
        *[f'--add-data={ap};{rp}' for ap, rp in [
            # (os.path.join(os.path.dirname(__file__), '*'), '.'),
            (os.path.join(os.path.dirname(sys.executable), "Lib", "site-packages"), '.'),
            (os.path.join(os.path.dirname(sys.executable), "Lib", "gmsh*"), '.'),
            ('main.qml', '.'),
            ('u', 'u'),
            ('icon', 'icon'),
            ('*.tdat', '.'),
            ('sphere0.25m.dat', '.'),
            ('delete.stl', '.'),
            # (os.path.join(os.path.dirname(sys.executable), "Library", "bin", "vtk*.dll"), '.'),
            (os.path.join(os.path.dirname(sys.executable), "Library", "bin"), '.'),
        ]],
        # *[f'--add-data={ap};{rp}' for ap, rp in uiFilesMap],
        # *[f'--hidden-import={model}' for model in [
        #     'numpy', 'vtk', 'vtkmodules', 'sip', 'PyQt5.sip', 'traitsui', 'gmsh'
        # ]],
        # f'-p={os.path.join(os.path.dirname(sys.executable), "Lib", "site-packages")}',
        # '--noupx',
        '--clean',
        'main.py',
        # '--name=hems',
        '-y'
    ]
    # ]
    run(opt)
    # copyFilesMap = []
    # for filedir, dirnames, filenames in os.walk(os.path.dirname(__file__)):
    #     for filename in filenames:
    #         fileType = os.path.splitext(filename)[-1].lower()
    #         if fileType == '.ui' or fileType == '.stl':
    #             absPath = os.path.join(filedir, filename)
    #             relPath = os.path.relpath(absPath, os.path.dirname(__file__))
    #             if relPath.startswith('build') or relPath.startswith('dist') or relPath.startswith('pyqt6_python310'):
    #                 continue
    #             # print(relPath, absPath)
    #             copyFilesMap.append((absPath, relPath))
    #
    # for absPath, relPath in copyFilesMap:
    #     srcPath = absPath
    #     dstPath = os.path.join(os.path.dirname(__file__), 'dist', relPath)
    #     print(dstPath)
    #     for preDir in os.path.split(os.path.relpath(dstPath, os.path.dirname(__file__)))[:-1]:
    #         try:
    #             os.mkdir(preDir)
    #         except Exception as e:
    #             # print(e)
    #             pass
    #     shutil.copyfile(srcPath, dstPath)

